package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.DeliveryAddressDao;
import com.foreknow.elm.dao.impl.DeliveryAddressDaoImpl;
import com.foreknow.elm.po.Deliveryaddress;
import com.foreknow.elm.util.DBUtil;

import java.util.ArrayList;
import java.util.List;

public class DeliveryAddressServiceImpl implements DeliveryAddressDao {
    @Override
    public List<Deliveryaddress> listDeliveryAddressByUserId(String userId) throws Exception {
        List<Deliveryaddress> list = new ArrayList<>();
        DeliveryAddressDao deliveryAddressDao = new DeliveryAddressDaoImpl();
        try {
            DBUtil.getConnection();
            list = deliveryAddressDao.listDeliveryAddressByUserId(userId);
        }finally {
            DBUtil.close();
        }
        return list;
    }

    @Override
    public Deliveryaddress getDeliveryAddressById(Integer daId)  {
        Deliveryaddress deliveryaddress =null;
        DeliveryAddressDao deliveryAddressDao = new DeliveryAddressDaoImpl();
        try {
            try {
                deliveryaddress = deliveryAddressDao.getDeliveryAddressById(daId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }finally {
            DBUtil.close();
        }

        return deliveryaddress;
    }

    @Override
    public int saveDeliveryAddress(Deliveryaddress deliveryaddress) {
        DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
        int result = 0;
        try {
            DBUtil.beginTransaction();
            result = dao.saveDeliveryAddress(deliveryaddress);
            DBUtil.commitTransaction();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }finally {
                DBUtil.close();
            }
        }
        return result;
    }


    @Override
    public int updateDeliveryAddress(Deliveryaddress deliveryaddress) {
        DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
        int result = 0;
        try {
            DBUtil.beginTransaction();
            result = dao.updateDeliveryAddress(deliveryaddress);
            DBUtil.commitTransaction();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }finally {
                DBUtil.close();
            }
        }
        return result;
    }

    @Override
    public int removeDeliveryAddress(Integer daId) {
        DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
        int result = 0;
        try {
            DBUtil.beginTransaction();
            result = dao.removeDeliveryAddress(daId);
            DBUtil.commitTransaction();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }finally {
                DBUtil.close();
            }
        }
        return result;
    }

}
